import type { Component } from "vue"
import type { RouteRecordRaw,NavigationGuardNext, RouteLocationNormalizedLoadedGeneric } from "vue-router"

/**
 * @description 路由中间件
 * @param to
 * @param from
 * @param next
 * @example
 */
export const middleware = (to:RouteLocationNormalizedLoadedGeneric, from:RouteLocationNormalizedLoadedGeneric, next:NavigationGuardNext) => {
  next()
}

/**
 * @description 路由数据
 */
interface RouteDictionary {
  [key: string]: RouteRecordRaw
}
// interface RouteMeta {
//   title: string,
//   authRequere: boolean,
//   description: string,
//   root: string,
//   keywords: string,
//   layout:string,
//   parent:string
// }
const generalRoutesHandle = ():RouteDictionary => {
  const routeMap:RouteDictionary = {}
  const comPath = import.meta.glob('@/views/**/index.tsx');
  const metaPath = import.meta.glob('@/views/**/meta.json', {eager: true, as: 'json'});
  for (const path in comPath) {
    const pathArr = path.split('/');
    const pathName = pathArr[pathArr.length - 2];
    const meta = path.replace('index.tsx','meta.json');
    console.log('meta:',meta,'path:',path);

    const route:RouteRecordRaw = {
      path: `/${pathName}`,
      name: pathName,
      component: comPath[path] as Component,
      children: [],
      meta: metaPath[meta],
    }
    routeMap[pathName] = route;
  }
  console.log('routeMap',routeMap);

  return routeMap
}

generalRoutesHandle()
/**
 * @description 路由格式化
 * @returns 路由数据
 * @example
 */
export const routeFormat = ():RouteRecordRaw[] => {
  const routeMap = generalRoutesHandle()
  const routes:RouteRecordRaw[] = [
    {
      path: '/',
      name: 'index',
      redirect: '/home',
      children:[

      ]
    },
  ]
  console.log('routeMap',routeMap);
  return routes
}
